package com.nq.service;

import cn.hutool.core.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class MailService {

    @Autowired(required = false)
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String userName;

    /**
     * 发送信息邮件
     * @param to 收件人
     * @param subject 邮箱标题
     * @param content 邮箱内容
     * @throws MessagingException
     */
    public void sendMail(String to, String subject, String content) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        //邮箱发送者
        helper.setFrom(userName);
        //收件人，可以为多个收件人，收件人之间用逗号隔开
        helper.setTo(to);
        // 邮箱标题
        helper.setSubject(subject);
        // 邮箱内容
        helper.setText(content, true);
        mailSender.send(message);
    }

}
